package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pojo.Notice;
import service.NoticeService;

import java.util.List;

@Api(value = "通知公告管理", description = "医院通知公告相关接口")
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @ApiOperation(value = "查询所有通知", notes = "获取医院所有通知公告信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回通知列表", response = Notice.class, responseContainer = "List")
    })
    @GetMapping("/findAll")
    public ResponseEntity<List<Notice>> findAll() {
        return ResponseEntity.ok(noticeService.findAll());
    }

    @ApiOperation(value = "根据ID查询通知", notes = "通过ID获取单个通知公告信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回通知", response = Notice.class),
            @ApiResponse(code = 404, message = "未找到对应通知")
    })
    @GetMapping("/findById/{id}")
    public ResponseEntity<Notice> findById(@PathVariable Integer id) {
        Notice notice = noticeService.findById(id);
        return notice != null ? ResponseEntity.ok(notice) : ResponseEntity.notFound().build();
    }

    @ApiOperation(value = "新增通知", notes = "创建一个新的通知公告")
    @ApiResponses({
            @ApiResponse(code = 201, message = "通知创建成功"),
            @ApiResponse(code = 400, message = "无效的通知数据")
    })
    @PostMapping("/addNotice")
    public ResponseEntity<Void> addNotice(@RequestBody Notice notice) {
        noticeService.addNotice(notice);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @ApiOperation(value = "更新通知信息", notes = "根据通知ID更新信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "通知信息更新成功"),
            @ApiResponse(code = 404, message = "未找到对应通知")
    })
    @PostMapping("/updateNotice")
    public ResponseEntity<Void> updateNotice(@RequestBody Notice notice) {
        int success = noticeService.updateNotice(notice);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }

    @ApiOperation(value = "删除通知", notes = "根据通知ID删除记录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "通知删除成功"),
            @ApiResponse(code = 404, message = "未找到对应通知")
    })
    @DeleteMapping("/deleteNotice/{id}")
    public ResponseEntity<Void> deleteNotice(@PathVariable Integer id) {
        int success = noticeService.deleteNotice(id);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }
}